package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.article.model.UpdateArticleMess;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.AppThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;
    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDto dto) {
        if(dto==null||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否是游客
        ApUser apUser = AppThreadLocal.getApUser();
        Integer id = apUser.getId();
        if(id==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String articleLike="USER_UNLIKE_"+dto.getArticleId();
        Boolean hasKey = redisTemplate.opsForHash().hasKey(articleLike,id+"");

        if(hasKey&&dto.getType()==0){
            return ResponseResult.okResult("已经加入不喜欢的队列中");
        }
        //取消点赞   删除redis和mongodb
        if(hasKey&&dto.getType()==1){
            redisTemplate.opsForHash().delete(articleLike, apUser.getId()+"");
            Query query = Query.query(Criteria.where("userId").is(apUser.getId().longValue()).and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApLikesBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        if(!hasKey&&dto.getType()==0){
            //在redis和mongodb中添加数据
            redisTemplate.opsForHash().put(articleLike, id+"", "1");
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setType(Short.parseShort(0+""));
            apUnlikesBehavior.setUserId(apUser.getId().longValue());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            mongoTemplate.save(apUnlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setArticleId(dto.getArticleId());
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        updateArticleMess.setAdd(-1);
        //将行为发送至kafka
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(updateArticleMess));
        return ResponseResult.okResult(null);
    }
}
